package com.wxxymaker.edu.service;

import com.blade.ioc.annotation.Bean;
import com.wxxymaker.edu.model.entity.PracticeOption;
import io.github.biezhi.anima.Anima;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @AUTHOR soft
 * @DATE 2018/10/13 16:24
 * @DESCRIBE
 */
@Bean
public class PracticeOptionService implements Service<PracticeOption> {

    public List<PracticeOption> getOptions(int praId) {
        return Anima.select().from(PracticeOption.class)
                .where(PracticeOption::getPracticeId, praId).all();
    }

    public List<PracticeOption> addAll(List<PracticeOption> practiceOptions, int ptid) {
        return practiceOptions.stream().map(opt->{
            opt.setPracticeId(ptid);
            return add(opt);
        }).collect(Collectors.toList());
    }

    @Override
    public PracticeOption add(PracticeOption practiceOption) {
        Integer integer = practiceOption.save().asInt();
        if (integer>0) {
            practiceOption.setId(integer);
            return practiceOption;
        }
        return null;
    }

    @Override
    public PracticeOption delete(Object pk) {
        PracticeOption practiceOption = byId(pk);
        if (practiceOption !=null){
            practiceOption.delete();
            return practiceOption;
        }
        return null;
    }

    public boolean deleteByPid(Integer pid) {
        return Anima.delete().from(PracticeOption.class)
                .where(PracticeOption::getPracticeId, pid)
                .execute() > 0;
    }

    @Override
    public boolean change(PracticeOption practiceOption) {
        if (practiceOption.getId()>0) {
            return practiceOption.update() > 0;
        }
        return false;
    }

    @Override
    public PracticeOption byId(Object pk) {
        return Anima.select().from(PracticeOption.class).byId(pk);
    }
}
